using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using zhwy.saas.api.Entity.Sys;
using zhwy.saas.api.Models;
using zhwy.saas.api.Models.Auth;
using zhwy.saas.api.Services;
using zhwy.saas.api.Services.Sinterface;
using zhwy.saas.api.Services.Sinterface.Sys;

namespace zhwy.saas.api.Controllers.V1
{
    /// <summary>
    /// 授权
    /// </summary>
    [ApiVersion("1.0")]
    public class AuthController : BaseController<UsersEntity>
    {
        private readonly ITokenService _tokenService;
        private readonly ISysUserService _sysUserService;

        public AuthController(ISysUserService sysUserService,
            ITokenService tokenService)
            : base(sysUserService)
        {
            _tokenService = tokenService;
            _sysUserService = sysUserService;
        }

        /// <summary>
        /// 登录授权
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost("saas/admin/login")]
        public ApiResponseModel<SaasAdminLoginRes> SaasAdminLogin([FromBody] SaasAdminLoginReq request)
        {
            // TODO: 验证用户名密码
            if (request.Username == "admin" && request.Password == "123456")
            {
                var token = _tokenService.GenerateToken(
                    "1",
                    "3",
                    request.Username,
                    new[] { "admin" }
                );

                return GetResponseModel<SaasAdminLoginRes>(new SaasAdminLoginRes
                {
                    Token = token,
                    Username = request.Username
                }); ;
            }
            return GetResponseErrorModel<SaasAdminLoginRes>(null, 500, "用户名或密码错误");
        }
    }
}